﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Student_exambystep : StudentPageBase
{
    protected int id = 0;
    protected Exam.Core.Model.ExamPaper model = new Exam.Core.Model.ExamPaper();
    protected int qnumber = 0;
    protected int SEId = 0;
    protected StringBuilder sbtype = new StringBuilder();//保存题型
    protected StringBuilder sb = new StringBuilder();//保存题型和题目编号
    protected string paperfilepath = string.Empty;//试卷路径
    protected void Page_Load(object sender, EventArgs e) {
        if (!string.IsNullOrEmpty(Request.QueryString["id"])) {
            if (!int.TryParse(Request.QueryString["id"].Trim(), out id)) {
                Response.Write(Resources.labels.erro);
                Response.End();
            }
        }
        if (!IsPostBack) {

            //查询一下StudentPaper表是否存在我的考试
            string strSQL = "SELECT COUNT(*) FROM StudentPaper WHERE StudentID=" + isTheMe.StudentID.Trim() + " AND PaperID=" + id;
            object obj = Exam.Core.DAL.DataProvider.GetSingle(strSQL);
            if (null == obj || obj.ToString().Trim() == "0") { //不存在则添加
                strSQL = "INSERT INTO StudentPaper(StudentID,PaperID, PaperStatus)VALUES(" + isTheMe.StudentID.Trim() + "," + id + ",0)";
                Exam.Core.DAL.DataProvider.ExecuteSql(strSQL);
            }



            //判断考生是否已经考过并交卷了
            string SQL = "SELECT PaperStatus FROM StudentPaper WHERE StudentID=" + isTheMe.StudentID.Trim() + " AND PaperID=" + id;
            obj = Exam.Core.DAL.DataProvider.GetSingle(SQL);
            if (null != obj) {
                if (obj.ToString().Trim() == "2") {
                    Response.Redirect("finesh.aspx?id=1", true);//状态为2 表示已经考完
                }
            }
            else {
                Response.Redirect("finesh.aspx?id=2", true);//
            }
            //
            SQL = "SELECT ExamStatus FROM StudentExam WHERE StudentID=" + isTheMe.StudentID.Trim() + " AND PaperID=" + id;
            obj = Exam.Core.DAL.DataProvider.GetSingle(SQL);
            if (null != obj) {
                if (obj.ToString().Trim() == "2") {
                    Response.Redirect("finesh.aspx?id=1", true);//状态为2 表示已经考完
                }
            }


            getPaper();//获取试卷

            paperfilepath = model.PaperContent.Trim();

            for (int t = 0; t < model.ExamQuestionTypeList.Count; t++) {

                sbtype.Append(model.ExamQuestionTypeList[t].QuestionTypeId);
                sbtype.Append("|");

                System.Collections.Generic.List<Exam.Core.Model.ExamQuestion> objQs = model.ExamQuestionTypeList[t].QuestionList;
                for (int l = 0; l < objQs.Count; l++) {
                    sb.Append(model.ExamQuestionTypeList[t].QuestionTypeId + "_" + objQs[l].QuestionId + ",");
                }
                if (sb.Length > 0) {
                    sb.Length--;
                }
                sb.Append("|");
            }

            //将考生ID 与 试卷ID 开始时间 状态 写入考生考卷表
            SEId = new Exam.Core.DAL.StudentExam().StuBeginExam(id, isTheMe.StudentID, DateTime.Now.ToString(), 1);

        }
    }

    private bool ExamStatus(string sta) {
        if (sta.Trim() == "2") {
            return true;
        }
        return false;
    }


    private void getPaper() {
        model = new Exam.Core.DAL.Paper().getPaperById(id);
    }

    protected string getNumber(int number) {
        qnumber = qnumber + 1;//计算题目总数
        string result = number.ToString();
        StringBuilder sb = new StringBuilder();
        foreach (char c in result) {
            //<img src='../images/number/<%=getNumber(k+1) %>r.gif' border=0 />
            sb.Append("<img src='../images/number/" + c + "r.gif' border=0 />");
        }
        return sb.ToString();
    }
}